﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace DATA_SERVER
{
    class Display_Func
    {
        private static List<IPEndPoint> DisplayClient = new List<IPEndPoint>();
        private static List<Socket> DisplaySocket = new List<Socket>();
        private static bool isStop;
        private static TcpListener myListener;

        //发送消息给所有显示终端
        public static void SendToDisplay(Byte[] bSendData)
        {
            if (DisplaySocket.Count > 0)
            {
                for (int i = DisplaySocket.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        DisplaySocket[i].Send(bSendData, bSendData.Length, 0);  //尝试发送
                    }
                    catch
                    {
                        try
                        {
                            DisplaySocket[i].Connect(DisplayClient[i]);         //重连
                            DisplaySocket[i].Send(bSendData, bSendData.Length, 0);  //再次发送
                        }
                        catch
                        {
                            //重连失败后删除该显示终端
                            Data_Func.ShowText("显示终端 <" + DisplayClient[i].ToString() + "> 已断开");
                            DisplaySocket.RemoveAt(i);
                            DisplayClient.RemoveAt(i);
                        }
                    }
                }
            }
        }

        public static void StartService()
        {
            isStop = false;

            Thread listenThread = new Thread(new ThreadStart(StartListen));
            listenThread.IsBackground = true;
            listenThread.Start();
        }

        public static void StopService()
        {
            if (isStop)
            {
                return;
            }

            isStop = true;

            Thread.Sleep(1000);
            if (myListener != null)
            {
                myListener.Stop();
                myListener = null;
            }
        }

        //监听线程，接收显示终端的连接请求
        private static void StartListen()
        {
            Socket mySocket = null;
            while (!isStop)
            {
                try
                {
                    myListener = new TcpListener(IPAddress.Any, 9001);
                    myListener.Start();
                    break;
                }
                catch
                {
                    Thread.Sleep(10000);
                    continue;
                }
            }

            while (!isStop)
            {
                try
                {
                    mySocket = myListener.AcceptSocket();

                    DisplayClient.Add((IPEndPoint)mySocket.RemoteEndPoint);
                    DisplaySocket.Add(mySocket);
                    Data_Func.ShowText("显示终端 <" + mySocket.RemoteEndPoint.ToString() + "> 已连接");
                }
                catch { }
            }
        }
    }
}
